package com.aviator.detective.model.scorecard.function;

import java.util.Map;

public class WeightSumFunction implements CalculateFunction{
    @Override
    public Double calculate(Map<String, Double> scoreMap, Double baseValue, Map<String, Double> keyWeights) {
        if (scoreMap == null || scoreMap.size() == 0){
            return null;
        }
        if (baseValue == null){
            baseValue = 0.0;
        }
        double weightSumValue = baseValue;
        for (Map.Entry<String,Double> weightScoreEntry : scoreMap.entrySet()){
            String key = weightScoreEntry.getKey();
            double scoreValue = weightScoreEntry.getValue();
            Double weight = keyWeights.get(key);
            if (weight == null || weight == 0.0){
                throw new IllegalArgumentException("score table weight not be null or zero on key:"+key);
            }
            weightSumValue +=weightSumValue*weight;
        }
        return weightSumValue;
    }

    @Override
    public CalculateMode getMode() {
        return CalculateMode.WEIGHT_SUM;
    }
}
